package com.gm.commonlib.views.dialog.loadingDialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gm.commonlib.R;


public class LoadingDialog implements ILoadingDialog{
    private LoadingDialog(){};
    private static LoadingDialog loadingDialog=new LoadingDialog();
    public static LoadingDialog newLoadingDialog(){
        return loadingDialog;
    }
    static Dialog dialog;
    static TextView tipTextView;

    public  Dialog createDialog(Context context, String msg,
                                      boolean cancelable) {
        if (dialog!=null){
            return dialog;
        }
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.view_loading_dialog, null);
        tipTextView = view.findViewById(R.id.tv_message);
        tipTextView.setText(msg);
        dialog = new Dialog(context, R.style.loading_dialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        return dialog;

    }

    public  Dialog showDialog(Context context, String msg,
                                    boolean cancelable) {
        try {
            if (dialog != null && dialog.isShowing()) {
                return dialog;
            }
            dialog = createDialog(context, msg, cancelable);
            if (!TextUtils.isEmpty(msg) && tipTextView != null) {
                tipTextView.setVisibility(View.VISIBLE);
            }
            dialog.show();
            return dialog;
        } catch (Exception e) {
            return null;
        }

    }

    public  void hideLoadingDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
        }
    }
    public void onDestroy(){
        hideLoadingDialog();
        dialog=null;
        loadingDialog=null;
    }

    public  Dialog show(Context context) {
        return showDialog(context, null, false);
    }

    public  Dialog show(Context context, String msg) {
        return showDialog(context, msg, false);
    }

    @Override
    public void hid() {
        hideLoadingDialog();
    }

    public  Dialog show(Context context, int msgId) {
        return showDialog(context, context.getResources().getString(msgId), false);
    }

    public  Dialog show(Context context, String msg, boolean canOutsideCancel) {
        return showDialog(context, msg, canOutsideCancel);
    }

    public  Dialog show(Context context, boolean canCancel) {
        return showDialog(context, null, canCancel);
    }


}
